package com.fas.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class IllationResultActivity extends Activity implements OnClickListener {

	protected static final String TAG = "IllationResultActivity";
	private LinearLayout resultView;
	private Button btn_send;
	private Button btn_finish;
	private String result;
	private String pathogeny;
	private String control;
	TextView pathogenyText;
	TextView controlText;
	TextView l;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		FreshActivity.list.add(this);
		resultView = (LinearLayout) findViewById(R.id.linearLayout1);
		btn_send = (Button) findViewById(R.id.sendmessage);
		btn_finish = (Button) findViewById(R.id.finish);
		btn_send.setOnClickListener(this);
		btn_finish.setOnClickListener(this);

		result = getIntent().getStringExtra("result");
		pathogeny = getIntent().getStringExtra("pathogeny");
		control = getIntent().getStringExtra("control");

		addText(result, pathogeny, control, 20.f, Color.BLACK);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case 0:

			setTextSize(FreshActivity.S);
			break;
		case 1:

			setTextSize(FreshActivity.M);
			break;
		case 2:

			setTextSize(FreshActivity.L);
			break;
		}
		return super.onOptionsItemSelected(item);

	}

	private void setTextSize(int s2) {
		FreshActivity.nowSize = s2;
		l.setTextSize(FreshActivity.nowSize);
		pathogenyText.setTextSize(FreshActivity.nowSize);
		controlText.setTextSize(FreshActivity.nowSize);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		menu.add(0, 0, 1, "小号字体");
		menu.add(0, 1, 1, "中号字体");
		menu.add(0, 2, 1, "大号字体");
		return super.onCreateOptionsMenu(menu);

	}

	public void addText(String result, String pathogeny, String control,
			float textsize, int color) {
		l = new TextView(this);

		l.setGravity(Gravity.CENTER_HORIZONTAL);
		l.setText(result);
		l.setTextSize(FreshActivity.nowSize);
		l.setTextColor(Color.BLACK);
		resultView.addView(l);

		if (pathogeny != null) {
			pathogenyText = new TextView(this);
			pathogenyText.setText(pathogeny);
			pathogenyText.setGravity(Gravity.LEFT);
			pathogenyText.setPadding(10, 0, 0, 0);
			pathogenyText.setTextSize(FreshActivity.nowSize);
			pathogenyText.setTextColor(Color.BLACK);
			resultView.addView(pathogenyText);
		}

		if (control != null) {
			controlText = new TextView(this);
			controlText.setText(control);
			controlText.setGravity(Gravity.LEFT);
			controlText.setPadding(10, 0, 0, 0);
			controlText.setTextSize(FreshActivity.nowSize);
			controlText.setTextColor(color);
			resultView.addView(controlText);
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.sendmessage:
			Uri smsToUri = Uri.parse(String.format("smsto:%s", ""));
			Intent intent = new Intent(android.content.Intent.ACTION_SENDTO,
					smsToUri);
			intent.putExtra("sms_body", result + pathogeny + control);
			startActivity(intent);
			break;
		case R.id.finish:
			new AlertDialog.Builder(this)
					.setMessage("请选择您的下一步操作")
					.setPositiveButton("重新推理",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									Intent intent = new Intent(
											IllationResultActivity.this,
											FASProjectActivity.class);
									startActivity(intent);
									finish();
								}
							})
					.setNegativeButton("退出应用",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// finish();
									Log.e(TAG, "list size is "
											+ FreshActivity.list.size());
									for (Activity activity : FreshActivity.list) {
										if (activity != null) {
											activity.finish();
										}
									}
								}
							}).show();
			break;

		default:
			break;
		}
	}

}
